﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Noodles.Core.Model;
using Noodles.Core;
using Noodles.Core.Repositories;
using NHibernate;
using NoodlesCms.Configuration;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.Wcf;
using NoodlesCms.Configuration.Themes;
using Noodles.Core.DBSettings;
using Noodles.Core.Settings;
using System.Web.Hosting;
using MvcMiniProfiler;

using System.Configuration;
using System.Data.SqlServerCe;


namespace NoodlesCms
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static string extensionsPath;
        public static void BeforeApplicationStart()
        {
            extensionsPath = HostingEnvironment.MapPath("~/bin/Extensions") ?? string.Empty;
          //  Extensibility.EnableAspNetIntegration(extensionsPath);
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
        protected void Application_BeginRequest()
        {
            MiniProfiler.Start();
        }

        public static void RegisterRoutes(RouteCollection routes) 
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
           // routes.MapRoute(
           //    "Details", // Route name
           //    "Post/{id}/{SeoTitle}", // URL with parameters
           //     new { controller = "Post", action = "Details", SeoTitle = UrlParameter.Optional }, // URL Defaults
           //     new { id = @"\d+" } // 
           //);
            routes.MapRoute(
              "Slug", // Route name
              "Post/{SeoTitle}", // URL with parameters
              new { controller = "Post", action = "Details" } // Parameter defaults
              , // Parameter defaults
                new string[] { "NoodlesCms.Controllers" }
          );
            routes.MapRoute(
                      "Pages", // Route name
                      "Pages/{page}", // URL with parameters
                      new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
                      , // Parameter defaults
                     new string[] { "NoodlesCms.Controllers" }
                      
                  );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "NoodlesCms.Controllers" }
            );
            

        }
        private static IContainer BuildContainer()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();          
            builder.RegisterModule(new DatabaseModule());       
            builder.RegisterModule(new SettingsModule());          
            builder.RegisterModule(new ExtensionsModule(extensionsPath, RouteTable.Routes));         
            builder.RegisterModule(new ThemesModule());
            builder.RegisterModule(new RepositoriesModule());        
            return builder.Build();
        }
        protected void Application_Start()
        {
            string con= ConfigurationManager.ConnectionStrings["NoodlesDBConnectionString"].ConnectionString;
            string dbPath = HostingEnvironment.MapPath("~/App_Data/NoodlesDB.sdf");
            SqlCeEngine engine = new SqlCeEngine();
            //engine.EnsureVersion40(dbPath, con);
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            var container = BuildContainer();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            AutofacHostFactory.Container = container;
          
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new NoodlesViewEngine());
            ControllerBuilder.Current.SetControllerFactory(new WebControllerFactory(container));
            ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
            
        }
        protected void Application_EndRequest()
        {
            MiniProfiler.Stop();
        }
    }
}